package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.BtyShop;
import cn.stylefeng.guns.sys.modular.system.entity.BtyShopTree;
import cn.stylefeng.guns.sys.modular.system.entity.CompanyInfo;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyShopTreeMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-07
 */
@Service
public class BtyShopTreeService extends ServiceImpl<BtyShopTreeMapper, BtyShopTree> {
    private Logger logger = LoggerFactory.getLogger(BtyShopTreeService.class);
    private RestTemplate restTemplate = new RestTemplate();
    @Resource
    private BtyShopTreeMapper btyShopTreeMapper;

    @Autowired
    private BtyShopService btyShopService;

    @Autowired
    private CompanyInfoService companyInfoService;

    /**
     * 新增部门
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:00 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public void addBtyShopTree(BtyShopTree btyShopTree) {

        if (ToolUtil.isOneEmpty(btyShopTree, btyShopTree.getSimpleName(), btyShopTree.getFullName(), btyShopTree.getPid(), btyShopTree.getDescription())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.btyShopTreeSetPids(btyShopTree);

        this.save(btyShopTree);
    }

    /**
     * 修改部门
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:00 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public void editBtyShopTree(BtyShopTree btyShopTree) {

        if (ToolUtil.isOneEmpty(btyShopTree, btyShopTree.getShopId(), btyShopTree.getSimpleName(), btyShopTree.getFullName(), btyShopTree.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.btyShopTreeSetPids(btyShopTree);

        this.updateById(btyShopTree);
    }

    /**
     * 删除门店
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    @Transactional
    public void deleteBtyShopTree(Long btyShopTreeId) {
        BtyShopTree btyShopTree = btyShopTreeMapper.selectById(btyShopTreeId);


        this.removeById(btyShopTree.getShopId());
    }

    /**
     * 获取ztree的节点列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    /**
     * 获取layuiTree的节点列表
     *
     * @author fengshuonan
     * @Date 2019-8-27 15:23
     */
    public List<LayuiTreeNode> layuiTree() {
        return this.baseMapper.layuiTree();
    }

    /**
     * 获取部門名稱
     */
    public String name(Long btyShopTreeId) {
        return this.baseMapper.name(btyShopTreeId);
    }

    /**
     * 获取ztree的节点列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public List<TreeviewNode> treeviewNodes() {
        return this.baseMapper.treeviewNodes();
    }

    /**
     * 获取所有部门列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public Page<Map<String, Object>> list(String condition, Long btyShopTreeId) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition, btyShopTreeId);
    }

    /**
     * 设置部门的父级ids
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:58 PM
     */
    private void btyShopTreeSetPids(BtyShopTree btyShopTree) {
        if (ToolUtil.isEmpty(btyShopTree.getPid()) || btyShopTree.getPid().equals(0L)) {
            btyShopTree.setPid(0L);
            btyShopTree.setPids("[0],");
        } else {
            Long pid = btyShopTree.getPid();
            BtyShopTree temp = this.getById(pid);
            String pids = temp.getPids();
            btyShopTree.setPid(pid);
            btyShopTree.setPids(pids + "[" + pid + "],");
        }
    }


    /**
     * 为bty_shop_tree中的店铺写入店铺编号
     */
    public void setShopCodes() {
        List<BtyShop> btyShopList = btyShopService.list();
        QueryWrapper<BtyShopTree> queryWrapper = new QueryWrapper<>();
        List<BtyShopTree> btyShopTreeList = btyShopTreeMapper.selectList(queryWrapper);
        for (BtyShopTree btyShopTree : btyShopTreeList) {
            for (BtyShop shop : btyShopList) {
                if (shop.getShopName().equals(btyShopTree.getSimpleName())) {
                    btyShopTree.setShopCode(shop.getShopCode());
                    this.updateById(btyShopTree);
                }
            }
        }
    }


    /**
     * 初始化shopTree数据-根据门店数据和分公司档案数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void initShopTeeData() {
        List<BtyShopTree> saveList = new LinkedList<>();
        Long idNum = 1L;
        BtyShopTree btyShopTreeTop = new BtyShopTree();
        btyShopTreeTop.setShopId(idNum);
        btyShopTreeTop.setPid(0L);
        btyShopTreeTop.setPids("[0],");
        btyShopTreeTop.setSimpleName("【企业】半天妖");
        btyShopTreeTop.setFullName("【企业】半天妖");
        saveList.add(btyShopTreeTop);
        //分公司档案数据
        QueryWrapper<CompanyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", "2c915b7b6aee33cd016af8be25ba69f9");
        List<CompanyInfo> companyInfoList = companyInfoService.list(queryWrapper);
        Map<String, BtyShopTree> btyShopTreeMap = new HashMap<>();
        btyShopTreeMap.put("2c915b7b6aee33cd016af8be25ba69f9", btyShopTreeTop);
        for (CompanyInfo companyInfo : companyInfoList) {
            BtyShopTree btyShopTree = new BtyShopTree();
            idNum = idNum + 1;
            btyShopTree.setShopId(idNum);
            btyShopTree.setPid(btyShopTreeTop.getShopId());
            btyShopTree.setPids(btyShopTreeTop.getPids() + "[" + btyShopTreeTop.getShopId() + "],");
            btyShopTree.setSimpleName(companyInfo.getName());
            btyShopTree.setFullName(companyInfo.getName());
            btyShopTreeMap.put(companyInfo.getId(), btyShopTree);
            saveList.add(btyShopTree);
        }

        //门店
        List<BtyShop> shopList = btyShopService.list();
        for (BtyShop shop : shopList) {
            if (shop.getEnableFlag() != 0) {
                BtyShopTree btyShopTree = new BtyShopTree();
                idNum = idNum + 1;
                btyShopTree.setShopId(idNum);
                BtyShopTree fuBranch = btyShopTreeMap.get(shop.getParentId());
                btyShopTree.setPid(fuBranch.getShopId());
                btyShopTree.setPids(fuBranch.getPids() + "[" + fuBranch.getShopId() + "],");
                btyShopTree.setSimpleName(shop.getShopName());
                btyShopTree.setFullName(shop.getShopName());
                btyShopTree.setShopCode(shop.getShopCode());
                saveList.add(btyShopTree);
            }
        }
        //保存前清除全部
        this.baseMapper.truncateData();
        //保存
        this.saveBatch(saveList);

    }


    /**
     * 获取门店信息
     * @param shopId
     * @return
     */
    public BtyShopTree getOneShopById(String shopId) {
        return this.baseMapper.getOneShopById(Long.valueOf(shopId));
    }
}
